import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:msh_checkbox/msh_checkbox.dart';
import 'package:wk_robot2/app/event/MsgContentEvent.dart';
import 'package:wk_robot2/app/modules/componentview/controllers/IndexViewController.dart';
import 'package:wk_robot2/app/storage/StorageManager.dart';

import '../../../const/AppConst.dart';
import '../../../event/DataCallBack.dart';
import '../../../nets/NetWorkManager.dart';
import '../../../routes/app_pages.dart';
import '../../../style/app_colors.dart';
import '../../../widget/CommonWidgets.dart';
import '../../../widget/tips.dart';
import '../controllers/login_controller.dart';

class LoginView extends GetView<LoginController> {
  LoginView({super.key});
  final GlobalKey _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {


    final TextStyle linkStyle = TextStyle(
        color: Colors.blue,
        fontSize: 26.sp
    );

    final TextStyle normal = TextStyle(
        color: AppColors.Sub_Title_Color,
        fontSize: 26.sp
    );

    return  Scaffold(
      backgroundColor: AppColors.app_base_bgcolor_color,
      body: Obx(()=>SingleChildScrollView(
        scrollDirection: Axis.vertical,
        controller: controller.scrollController.value,
        child: Stack(
          children: [
            Column(
              children: [
                Container(
                  width: MediaQuery.of(context).size.width,
                  child: Image.asset('assets/images/bg_login.webp',
                    width:  MediaQuery.of(context).size.width,
                    height: 703.h,
                    fit: BoxFit.fitWidth,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Stack(
                      children: [
                        Container(
                          margin: EdgeInsets.only(top:38),
                          child: CommonWidgets.getALine(),
                        ),
                        Text('登录',style: TextStyle(fontSize: 72.sp,fontWeight: FontWeight.bold),)
                        //Text('登录',style: TextStyle(fontSize: 72.sp,fontWeight: FontWeight.bold),),
                      ],
                    )
                  ],
                ),
                SizedBox(height: 40.h),
                Container(
                  child: Form(
                    key: _formKey,
                    child:  Container(
                      padding: EdgeInsets.all(20),
                      child: Column(
                        children: <Widget>[
                          Container(
                            width: 600.w,
                            height: 90.h,
                            decoration: BoxDecoration(
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.black.withOpacity(0.1),
                                  spreadRadius: 2.w,
                                  blurRadius: 7.w,
                                  offset: Offset(0, 8.sp), // changes position of shadow
                                ),
                              ],
                              color:Colors.white,
                              borderRadius: BorderRadius.circular(45), // Optional: add border radius
                            ),
                            child: TextFormField(
                              focusNode: controller.phoneFocusNode,
                              enabled: controller.phoneTextFieldEditable.value,
                              decoration: InputDecoration(
                                hintText: '请输入手机号',
                                suffixIcon: controller.suffixIconsVisible.value ? Transform.scale(
                                  scale: 0.5, // 调整缩放比例
                                  child: GestureDetector(
                                    onTap: () {
                                      /*phoneFocusNode.requestFocus();
                                      validateFocusNode.unfocus();*/
                                      controller.accountController.value.text = '';
                                      controller.setSuffixIconsVisible(false);
                                    },
                                    child: Image.asset('assets/images/ic_clear.webp'),
                                  ),
                                ): SizedBox(),
                              ),
                              controller: controller.accountController.value,
                              onChanged: (v) {
                                  controller.onTextFieldChange(v);
                              },
                              keyboardType: TextInputType.number,
                              inputFormatters: [FilteringTextInputFormatter.digitsOnly]

                            ),
                          ),
                          const SizedBox(height: 20),
                          Container(
                            width: 600.w,
                            height: 90.h,
                            decoration: BoxDecoration(
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.black.withOpacity(0.1),
                                  spreadRadius: 2.w,
                                  blurRadius: 7.w,
                                  offset: Offset(0, 8.sp), // changes position of shadow
                                ),
                              ],
                              color:Colors.white,
                              borderRadius: BorderRadius.circular(45), // Optional: add border radius
                            ),
                            child: TextFormField(
                              focusNode: controller.validateFocusNode,
                              decoration: InputDecoration(
                                  hintText: '请输入验证码',
                                  suffixIcon: GestureDetector(
                                    onTap: () {
                                      controller.validateFocusNode.requestFocus();
                                      controller.setSuffixIconsVisible(false);
                                      controller.startRequestValidateCode();
                                    },
                                    child: Container(
                                      margin: EdgeInsets.only(top:24.h,right: 36.w),
                                      child: Text(controller.buttonText.value,style: TextStyle(fontSize: 30.sp,color:
                                      controller.buttonText.value == '获取验证码' ? AppColors.BLUE_COLOR_CHARACTERS:AppColors.Sub_Title_Color)),
                                    ),
                                  )
                              ),
                              controller: controller.validateCodeController.value,
                              onChanged: controller.onValidateCodeChange,
                              keyboardType: TextInputType.number,
                              inputFormatters: [FilteringTextInputFormatter.digitsOnly]
                            ),
                          ),
                          //爱好
                          const SizedBox(height: 20),
                          Container(
                            margin: const EdgeInsets.only(top: 50),
                            width: 594.w,
                            child: ElevatedButton(
                                onPressed: controller.isSubmitBtnEnable.value == false ? null: () {
                                  if ((_formKey.currentState as FormState).validate()) {
                                    if (!controller.checkBoxChecked.value) {
                                      CommonWidgets.showConfirmDialog(context, '用户协议及隐私保护', '为了更好得保护您的权益,请您阅读并同意以下协议《用户协议》,《隐私政策》',
                                          '同意', '拒绝', DataCallBack(
                                            confirmCallBack: (v) {
                                                controller.requestLogin();
                                            },
                                            cancleCallBack: (v) {
                                                //do nothing.

                                            }
                                          ));

                                      return;


                                    }

                                    controller.requestLogin();


                                  }
                                },
                                child: const Text("登录")
                            ),
                          ),
                          SizedBox(height: 53.h),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Row(
                                mainAxisAlignment:MainAxisAlignment.start,
                                children: [
                                  SizedBox(width: 53.w),
                                  Obx(()=> MSHCheckbox(
                                    size: 40.w,
                                    value: controller.checkBoxChecked.value,
                                    colorConfig: MSHColorConfig.fromCheckedUncheckedDisabled(
                                      checkedColor: AppColors.Page_Title_Color,
                                    ),
                                    style: MSHCheckboxStyle.stroke,
                                    onChanged: (selected) {
                                      controller.checkBoxChecked.value = selected;
                                      controller.onCheckBoxChange();
                                    },
                                  )),
                                  SizedBox(width: 20.w,),
                                  RichText(
                                    text: TextSpan(
                                        children: [
                                          TextSpan(text: '我已阅读并同意，', style: normal),
                                          TextSpan(text: '用户协议',style: linkStyle,recognizer: TapGestureRecognizer()..onTap=controller.onTapUserAgreementTask),
                                          TextSpan(text: '，', style: normal),
                                          TextSpan(text: '隐私政策',style: linkStyle,recognizer: TapGestureRecognizer()..onTap=controller.onTapPrivacyPolicyTask)

                                        ]
                                    ),
                                  )
                                  // Text('我已经阅读并同意 用户协议 隐私政策'),
                                ],
                              ),
                              SizedBox(height: 10.h),
                              Text('未注册的手机号登录成功后将自动注册',style: normal,)
                            ],
                          ),
                          SizedBox(height: 100.h)
                        ],
                      ),
                    ),
                  ),)
              ],
            )
          ],
        ),//,
      )),
    );
  }


}
